<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="__static/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="__static/css/public.css" media="all">
    <link rel="stylesheet" href="__static/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }

        .text-right {
            text-align: right;
        }

        .layui-table {
            margin: 0;
        }

        .ew-tree-icon {
            display: none;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <blockquote class="layui-elem-quote">
            <p><i class="fa fa-window-maximize"></i> 系统菜单</p>
        </blockquote>
        <div>
            <div class="layui-row">
                <div class="layui-btn-group layui-col-md8">
                    <button class="layui-btn" id="btn-expand"><i class="fa fa-indent"></i> 全部展开</button>
                    <button class="layui-btn" id="btn-fold"><i class="fa fa-outdent"></i> 全部折叠</button>
                </div>
                <div class="layui-col-md4 text-right">
                    <button class="layui-btn layui-btn-normal" id="btn-new"><i class="fa fa-plus"></i> 新增菜单</button>
                </div>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="add">添加子项</a>
    <a class="layui-btn layui-btn-success layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="__static/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="__static/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['table', 'treeTable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treeTable = layui.treeTable;

        // 渲染表格
        var menuTree = treeTable.render({
            id: 'menu_list',
            elem: '#munu-table',
            skin: 'line',
            url: '/system/menu_list/t/json',
            page: false,
            tree: {
                iconIndex: 1,
                arrowType: 'arrow2',   // 自定义箭头风格
                isPidData: true,
                pidName: 'pid',
            },
            cols: [
                [
                    {type: 'numbers'},
                    {
                        field: 'title', minWidth: 200, title: '菜单名称'
                        // , templet: function(d) {
                        //     var icon = 'fa fa-chain';
                        //     if (d.icon) icon = d.icon;
                        //     else if (d.pid == 0) icon = 'fa fa-window-maximize';
                        //     else icon = 'fa fa-list';
                        //     return '<i class="' + icon + '"></i> ' + d.title;
                        // }
                    },
                    {
                        field: 'icon', width: 20, align: 'center', title: '图标', type: 'text'
                        , templet: function (d) {
                            if (d.icon) return '<i class="' + d.icon + '"></i>';
                            return '';
                        }
                    },
                    {field: 'href', width: 200, title: '菜单url'},
                    // {field: 'target', width: 80, title: '目标'},
                    {field: 'sort', width: 58, align: 'right', title: '排序'},
                    {
                        field: 'status', width: 70, align: 'center', title: '状态', type: 'text'
                        , templet: function (d) {
                            if (d.status == 'on') return '<span class="layui-badge layui-bg-blue">显示</span>';
                            else return '<span class="layui-badge-rim">隐藏</span>';
                        }
                    },
                    {
                        field: 'isMenu', width: 60, align: 'center', title: '类型',
                        templet: function (d) {
                            if (d.is_menu == 0)
                                return '<span class="layui-badge layui-bg-red">按钮</span>';
                            if (d.lv == 0)
                                return '<span class="layui-badge layui-bg-blue">模块</span>';
                            if (d.lv == 1)
                                return '<span class="layui-badge layui-bg-green">菜单</span>';
                            if (d.lv == 2)
                                return '<span class="layui-badge-rim">子项</span>';
                        }
                    },
                    {field: 'create_time', width: 162, title: '创建时间', type: 'text'},
                    {templet: '#auth-state', width: 200, align: 'center', title: '操作'}
                ]
            ]
        });

        $('#btn-expand').click(function () {
            menuTree.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            menuTree.foldAll('#munu-table');
        });

        $('#btn-new').click(function () {
            var index = layer.open({
                title: '添加菜单',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/system/menu_edit',
                end: function () {
                    menuTree.reload();
                }
            });
        });

        //监听工具条
        treeTable.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                var confirm = layer.confirm('删除菜单 [' + data.title + ']？', {
                    yes: function (id) {
                        var url = '/system/menu_del/id/' + data.id;
                        $.get(url, function (d) {
                            menuTree.reload();
                            layer.msg(d.msg, {
                                time: 500
                            });
                        });
                        layer.close(confirm);
                    }
                });
            } else if (layEvent === 'edit') {
                var index = layer.open({
                    title: '修改菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/system/menu_edit/id/' + data.id,
                    end: function () {
                        menuTree.reload();
                    }
                });
            } else if (layEvent === 'add') {
                var index = layer.open({
                    title: '添加子项',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/system/menu_edit/pid/' + data.id,
                    end: function () {
                        menuTree.reload();
                    }
                });
            }
        });
    });
</script>
</body>
</html>